package tomcat_handler;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.sql.SQLException;

import IOTWeb.com.IOTMain;
import IOTWeb.com.utils.FileOption;
import IOTWeb.com.utils.H2ConnectionPool;
import IOTWeb.com.utils.Utils;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

public class LoadClass extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException{
		doPost(req, resp);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

		String body = "";
		String res = "";
		Utils utils = new Utils();
		response.setContentType("application/javascript;charset=UTF-8");
		response.setHeader("Access-Control-Allow-Origin", "*");
		String uri = request.getRequestURI();
		PrintWriter out = response.getWriter();
		String[] pathArray = uri.split("/");
		BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
		if((body = reader.readLine()) != null) {
			body=new String(body.getBytes("iso-8859-1"),"UTF-8");
		}
		String[] args= {pathArray[4], body};
		try {
			res = utils.loadClass(pathArray[3], args);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.write(res);
		out.close();
		
	}
}
